const Koa = require('koa')
const app=new Koa();

const router=require('koa-router');
const static = require('koa-static');
const path=require('path');

const connect = require('./db/index');
const {query} = require('./db/index');
const bodyparser = require('koa-bodyparser');

router.get('/list',async ctx=>
{
    let lists=[];
    let res=await new Promise((resolve,reject)=>
    {
        connect.query(`select * from user`,(err,data)=>
        {
            if(!err)
            {
                resolve(data)
            }
            else
            {
                reject('失败')
            }
        })
    })
    ctx.body={
        code:0,
        mes:'list',
        data:res
    }
})
router.get('/detail/:id',async ctx=>{
    console.log(ctx.params);
    ctx.body={
        code:0,
        mes:"detail接口"
    }
})
router.post('/login',async ctx=>
{
    console.log(ctx.reqest.body);
    let {name,phone}=ctx.request.body;
    let res=await new Promise((res,rej)=>{
        connect.query(`select * from user where name='${name}'`,(err,data)=>
        {
            if(!err)
            {
                res(data)
            }
            else
            {
                rej(err)
            }
        })
        ctx.body={
            code:0,
            mes:"登录成功",
            res
        }
    })
})
/* app.use(bodyParser())
app.use(router.routes())
app.use(router.alloweMethods())
app.use(static(path.join(__dirname,'public'))) */
app.listen(3000)